class Solution {
public:
    int timeRequiredToBuy(vector<int>& tickets, int k) {
        size_t i = 0;
        int count = 0;
        size_t n = tickets.size();
        while (tickets[k] != 0) {
            if (tickets[i] > 0) {
                ++count;
                tickets[i] -= 1;
            }
            ++i;
            if (i == n) {
                i = 0;
            }
        }
        return count;
    }
};